package com.wallet.my.service.impl;

import com.wallet.my.controller.model.LoginRequest;
import com.wallet.my.controller.model.RegisterRequest;
import com.wallet.my.dao.mapper.UserMapper;
import com.wallet.my.dao.pojo.UserDO;
import com.wallet.my.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * userservice的实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    public UserServiceImpl() {
        super();
    }
//注册用户新增进user表
    @Override
    public Map<String, Object> register(RegisterRequest registerRequest) {
        HashMap<String, Object> map = new HashMap<>();
        UserDO userByName = userMapper.getUserByName(registerRequest.getUsername());
        if (userByName==null){
            UserDO userDO = new UserDO();
            userDO.setUsername(registerRequest.getUsername());
            userDO.setPassword(registerRequest.getPassword());
            userMapper.addUser(userDO);
            map.put("code",200);
            map.put("message","注册成功，请登录");

        }else{
            map.put("code",100);
            map.put("message","用户名已存在");
        }
        return map;
    }

    @Override
    public Map<String, Object> login(LoginRequest loginRequest) {
        HashMap<String, Object> map = new HashMap<>();
        UserDO userByName = userMapper.getUserByName(loginRequest.getUsername());
        if (userByName==null){
            map.put("code",100);
            map.put("message","用户名或密码错误");
            return map;
        }else {
            if (userByName.getPassword().equals(loginRequest.getPassword())){
                map.put("code",200);
                map.put("message","登录成功");
            }else {
                map.put("code",100);
                map.put("message","用户名或密码错误");
            }
            return map;
        }
    }
}
